package ro.gateway.aida.tags;

import java.io.IOException;
import java.util.Calendar;
import java.util.Hashtable;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.Module_DB;
import ro.xblue.translator.TranslatorConstants;
import ro.xblue.translator.Translator_DB;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: ro-gateway</p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: DateSelectorTag.java,v 1.1 2005/07/20 12:08:58 mihaipostelnicu Exp $
 */
public class DateSelectorTag extends TagSupport
{
      private String zname;
      private String lname;
      private String aname;
      private long current_value;
      private String lang;

      public DateSelectorTag()
      {
            current_value = -1;
      }

      /**
       *
       */
      public int doStartTag() throws JspException
      {
            return ( SKIP_BODY );
      }

      /**
       *
       */
      public int doEndTag() throws JspException
      {
            StringBuffer sb = new StringBuffer();

            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis( current_value );
            int day = cal.get( Calendar.DAY_OF_MONTH );
            int month = cal.get( Calendar.MONTH );
            int year = cal.get( Calendar.YEAR );
            if ( current_value == -1 )
            {
                  day = -1;
                  month = -1;
                  year = -1;
            }

            if ( lang == null )
            {
                  LanguageBean lb = ( LanguageBean ) pageContext.getSession().
                        getAttribute( TranslatorConstants.BLUE_CLANG );
                  if ( lb != null )
                  {
                        lang = lb.language;
                  }
                  else
                  {
                        lang = "ro";
                  }
            }
            String[] months = ( String[] ) MONTHS.get( lang );
            String[] labels = ( String[] ) LABELS.get( lang );

            if ( months == null )
            {
                  Translator_DB translator = ( Translator_DB ) pageContext.getServletContext().
                        getAttribute( TranslatorConstants.BLUE_TRANSLATOR );
                  Hashtable msgs = null;
                  if ( translator != null )
                  {
                        Module_DB module = translator.getModule( "mod_date" );
                        if ( module != null ) msgs = module.getMessages( lang );
                  }

                  if ( msgs == null )
                  {
                        months = en_months;
                  }
                  else
                  {
                        months = new String[ 12 ];
                        months[ 0 ] = ( String ) msgs.get( "m.jan" );
                        months[ 1 ] = ( String ) msgs.get( "m.feb" );
                        months[ 2 ] = ( String ) msgs.get( "m.mar" );
                        months[ 3 ] = ( String ) msgs.get( "m.apr" );
                        months[ 4 ] = ( String ) msgs.get( "m.may" );
                        months[ 5 ] = ( String ) msgs.get( "m.jun" );
                        months[ 6 ] = ( String ) msgs.get( "m.jul" );
                        months[ 7 ] = ( String ) msgs.get( "m.aug" );
                        months[ 8 ] = ( String ) msgs.get( "m.sep" );
                        months[ 9 ] = ( String ) msgs.get( "m.oct" );
                        months[ 10 ] = ( String ) msgs.get( "m.nov" );
                        months[ 11 ] = ( String ) msgs.get( "m.dec" );
                        MONTHS.put( lang, months );
                  }
            }

            if ( labels == null )
            {
                  Translator_DB translator = ( Translator_DB ) pageContext.getServletContext().
                        getAttribute( TranslatorConstants.BLUE_TRANSLATOR );

                  Hashtable msgs = null;
                  if ( translator != null )
                  {
                        Module_DB module = translator.getModule( "mod_date" );
                        if ( module != null ) msgs = module.getMessages( lang );
                  }

                  if ( msgs == null )
                  {
                        labels = en_labels;
                  }
                  else
                  {
                        labels = new String[ 3 ];
                        labels[ 0 ] = ( String ) msgs.get( "label.day" );
                        labels[ 1 ] = ( String ) msgs.get( "label.month" );
                        labels[ 2 ] = ( String ) msgs.get( "label.year" );
                  }
            }

            sb.append( "<select name=\"" ).
                  append( zname ).
                  append( "\">" ).
                  append( "<option value=\"-1\">" ).
                  append( labels[ 0 ] ).
                  append( "</option>" );
            for ( int i = 1; i <= 31; i++ )
            {
                  sb.append( "<option value=\"" ).
                        append( i ).
                        append( "\" " );
                  if ( i == day )
                  {
                        sb.append( "selected=\"true\"" );
                  }
                  sb.append( ">" ).
                        append( i ).
                        append( "</option>" );
            }
            sb.append( "</select>\n<select name=\"" ).
                  append( lname ).
                  append( "\">" ).
                  append( "<option value=\"-1\">" ).
                  append( labels[ 1 ] ).
                  append( "</option>" );
            for ( int i = 0; i < 12; i++ )
            {
                  sb.append( "<option value=\"" ).
                        append( i ).
                        append( "\" " );
                  if ( i == month )
                  {
                        sb.append( "selected=\"true\"" );
                  }
                  sb.append( ">" ).
                        append( months[ i ] ).
                        append( "</option>" );
            }
            sb.append( "</select>\n<select name=\"" ).
                  append( aname ).
                  append( "\">" ).
                  append( "<option value=\"-1\">" ).
                  append( labels[ 2 ] ).
                  append( "</option>" );
            for ( int i = 1970; i <= 2025; i++ )
            {
                  sb.append( "<option value=\"" ).
                        append( i ).
                        append( "\" " );
                  if ( i == year )
                  {
                        sb.append( "selected=\"true\"" );
                  }
                  sb.append( ">" ).
                        append( i ).
                        append( "</option>" );
            }
            sb.append( "</select>" );

            JspWriter writer = pageContext.getOut();
            try
            {
                  writer.println( sb.toString() );
            }
            catch ( IOException ioEx )
            {
                  System.err.println( ioEx );
            }

            lang = null;
            zname = null;
            lname = null;
            aname = null;
            current_value = -1;

            return ( EVAL_PAGE );
      }

      public void setZname( String zname )
      {
            this.zname = zname;
      }

      public String getZname()
      {
            return zname;
      }

      public void setLname( String lname )
      {
            this.lname = lname;
      }

      public String getLname()
      {
            return lname;
      }

      public void setAname( String aname )
      {
            this.aname = aname;
      }

      public String getAname()
      {
            return aname;
      }

      public void setCurrent_value( long current_value )
      {
            this.current_value = current_value;
      }

      public long getCurrent_value()
      {
            return current_value;
      }

      public void release()
      {
            System.err.println( "releasing" );
            current_value = -1;
      }

      public void setLang( String lang )
      {
            this.lang = lang;
      }

      public String getLang()
      {
            return lang;
      }

      public static final String[] ro_months = new String[]{
            "Ianuarie", "Februarie", "Martie", "Aprilie", "Mai", "Iunie", "Iulie",
            "August", "Septembrie", "Octombrie", "Noiembrie", "Decembrie"
      };
      public static final String[] en_months = new String[]{
            "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December"
      };
      public static final String[] ro_labels = new String[]{
            "Ziua", "Luna", "Anul"
      };
      public static final String[] en_labels = new String[]{
            "Day", "Month", "Year"
      };

      public static java.util.Hashtable MONTHS;
      public static java.util.Hashtable LABELS;

      static
      {
            MONTHS = new Hashtable();
            MONTHS.put( "ro", ro_months );
            MONTHS.put( "rn", en_months );

            LABELS = new Hashtable();
            LABELS.put( "ro", ro_labels );
            LABELS.put( "en", en_labels );
      }
}